<!--
 * @Author: zhangxiangsheng 1026583651@qq.com
 * @Date: 2024-06-18 13:57:45
 * @LastEditors: zhangxiangsheng 1026583651@qq.com
 * @LastEditTime: 2024-06-21 16:42:28
 * @FilePath: \appGly\pages\ChargeCar\ChargeCarEdt.vue
 * @Description: 
 * 
 * Copyright (c) 2024 by ${git_name_email}, All Rights Reserved. 
-->
<template>
	<view>
		<u-navbar title="设备查看"></u-navbar>
		<formz ref="formz" v-if="formshow" :inputs="inputs" :data="data"></formz>
		<fixedbtn>
			<u-button type="error" @click="del" :loading="loading">
				{{ $z.$t("刪除设备") }}
			</u-button>
			<view style="width: 40rpx"></view>
			<u-button type="primary" @click="save" :loading="loading">
				{{ $z.$t("修改设备") }}
			</u-button>
		</fixedbtn>
		<zmodal ref="zmodal"></zmodal>
	</view>
</template>

<script>
	// import json from "./indexjson.js";
	export default {
		data() {
			return {
				inputs: [],
				loading: false,
				json: {},
				query: {},
				data: {},
				formshow: false,
			};
		},
		onLoad(query) {
			this.query = query;
			this.init();
		},
		methods: {
			async init() {
				await this.$store.dispatch("user/getuser");
				let json = await import("./indexjson.js");
				let ChargeCar = await this.$z.request("/ChargeCar", {
					gtel: this.query.gtel,
				});
				let ChargeCarSys = await this.$z.request("/ChargeCarSys", {
					gtel: this.query.gtel,
				});
				this.data = {
					...ChargeCar[0],
					...ChargeCarSys[0],
				};
				this.json = json.default;
				this.inputs = json.default.ChargeCareditjson;
				this.formshow = true;
				console.log(this.inputs);
			},
			del() {
				this.$refs.zmodal.init({
					content: this.$z.$t("确认删除此设备？"),
					showcancel: true,
					confirm: async () => {
						await this.$z.request("/ChargeCardel", {
							gtel: this.data.gtel,
						});
						this.$z.back();
					},
				});
			},
			async save() {
				let data = await this.$refs.formz.validate();
				if (!data.type) {
					data.type = "2";
				}
				if (data.type == "15") {
					data.df = Number(data.dfsj) + Number(data.qbf);
				}
				let errdatas = await this.json.checkdata(this.$u.deepClone(data));
				if (errdatas.length != 0) {
					this.$u.toast(errdatas[0]);
					return;
				}
				if (data.type == "3" || data.type == "8") {
					if (!this.sortkeyfn(data, "a")) {
						await this.msgbox({
							content: this.$z.$t("瓦数没有从低到高排序是否继续？"),
						});
					} else if (!this.sortkeyfn(data, "df", false)) {
						await this.msgbox({
							content: "充电时长没有从高到低排序是否继续？",
						});
					}
				} else if (data.type == "6") {
					if (!this.sortkeyfn(data, "a")) {
						await this.msgbox({
							content: this.$z.$t("瓦数没有从低到高排序是否继续？"),
						});
					} else if (!this.sortkeyfn(data, "df")) {
						await this.msgbox({
							content: this.$z.$t("时长费用没有从低到高排序是否继续？"),
						});
					}
				}
				this.loading = true;
				let par = await this.$z.request("/ChargeCaredt", data);

				console.log(data);
				if (par[0].par == "succeed") {
					if (data.type == "12" || data.type == "14") {
						await this.$z.request("/ChargeCfgdfedt", data);
					}
					uni.$u.toast(this.$z.$t("操作成功"));
					this.$z.back();
				} else {
					let str = config.pars[par[0].par] || "";
					uni.$u.toast(str);
					this.loading = false;
				}
			},
		},
	};
</script>

<style></style>